package Association_rules;
/**
 * 树类
 * @author  *
 * @param <T>
 */
public class Tree<T> {

    private Node<T> root;


    public Node<T> getRoot() {
        return root;
    }

    /**
     * 构造函数
     * @param root
     */
    public Tree(Node<T> root) {
        super();
        //  root.setParent(null);
        this.root = root;
    }
    public Tree(T rootCont) {
        super();
        this.root = new Node(rootCont);
    }

    /**
     * 增加新节点
     * @param curNode
     * @param newNode
     */
    public void addNewNode(Node<T> curNode,Node<T> newNode){
        if(newNode==null||curNode==null){
            return ;
        }
        newNode.setParent(curNode);
        curNode.nodeList.add(newNode);
    }
    /**
     * 遍历
     */
    public void traversal(Node<T> root){
        System.out.println(root.getCont());
        for(Node<T> tempNode:root.getNodeList() ){
            traversal(tempNode);
        }
    }
    /**
     * 查找
     * @param root
     * @param searchcont
     * @return
     */
    private boolean searchHelp(Node<T> root,Node<T> result,T searchcont){
        boolean success=false;
        if(root.getCont()==searchcont){
            result.setCont(root.getCont());
            result.setNodeList(root.nodeList);
            result.setParent(root.getParent());
            return true;
        }

        for(Node<T> tempNode:root.getNodeList() ){
            success=searchHelp(tempNode,result,searchcont);
        }
        return success;
    }

    public Node<T> search(Node<T> root,T searchcont){
        if(searchcont==null){return null;}
        Node<T> result=new Node<T>();
        searchHelp(root,result,searchcont);
        System.out.println("search completed");
        return result;
    }
}

